/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.core.util;

import org.springframework.lang.Nullable;

/**
 * 版本号比较工具
 *
 * @author kite@iiifi.com 花朝
 */

public class VersionUtils {

    private static final String DELIMITER = "\\.";

    /**
     * 比较版本号是否相同
     *
     * @param v1 v1
     * @param v2 v2
     * @return {boolean}
     */
    public boolean eq(@Nullable String v1, @Nullable String v2) {
        return compare(v1, v2) == 0;
    }

    /**
     * 比较版本号是不相同
     *
     * @param v1 v1
     * @param v2 v2
     * @return {boolean}
     */
    public boolean ne(@Nullable String v1, @Nullable String v2) {
        return compare(v1, v2) != 0;
    }

    /**
     * v1 是否大于 v2
     *
     * @param v1 v1
     * @param v2 v2
     * @return {boolean}
     */
    public boolean gt(@Nullable String v1, @Nullable String v2) {
        return compare(v1, v2) > 0;
    }

    /**
     * v1 是否大于等于 v2
     *
     * @param v1 v1
     * @param v2 v2
     * @return {boolean}
     */
    public boolean gte(@Nullable String v1, @Nullable String v2) {
        return compare(v1, v2) >= 0;
    }

    /**
     * v1 是否小于 v2
     *
     * @param v1 v1
     * @param v2 v2
     * @return {boolean}
     */
    public boolean lt(@Nullable String v1, @Nullable String v2) {
        return compare(v1, v2) < 0;
    }

    /**
     * v1 是否小于等于 v2
     *
     * @param v1 v1
     * @param v2 v2
     * @return {boolean}
     */
    public boolean lte(@Nullable String v1, @Nullable String v2) {
        return compare(v1, v2) <= 0;
    }

    /**
     * 比较2个版本号
     *
     * @param v1 v1
     * @param v2 v2
     * @return (v1 < v2) ? -1 : ((v1 == v2) ? 0 : 1)
     */
    private static int compare(@Nullable String v1, @Nullable String v2) {
        // null 视为最小版本
        if (v1 == null) {
            return -1;
        }
        if (v2 == null) {
            return 1;
        }
        if (v1.equals(v2)) {
            return 0;
        }
        // 去除空格
        v1 = v1.trim();
        v2 = v2.trim();
        if (v1.equals(v2)) {
            return 0;
        }
        String[] v1s = v1.split(DELIMITER);
        String[] v2s = v2.split(DELIMITER);
        int v1sLen = v1s.length;
        int v2sLen = v2s.length;
        int len = Math.max(v1sLen, v2sLen);
        for (int i = 0; i < len; i++) {
            String c1 = len > v1sLen || null == v1s[i] ? StringPool.EMPTY : v1s[i];
            String c2 = len > v2sLen || null == v2s[i] ? StringPool.EMPTY : v2s[i];

            int result = c1.compareTo(c2);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    }
}
